package com.e3mall.service.impl;

import com.e3mall.interfaces.ItemCatService;
import com.e3mall.manage.mapper.TbItemCatMapper;
import com.e3mall.manage.pojo.TbItemCat;
import com.e3mall.manage.pojo.TbItemCatExample;
import com.e3mall.utils.EasyUITreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者: Vgger
 * @E-Mail:1055106789@qq.com
 * @描述:
 * @修改时间: Created in 20:31 2018/4/24
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<EasyUITreeNode> getItemCatTreeNodesByParentId(long parentId) {

//        使用查询条件查询
        TbItemCatExample example = new TbItemCatExample();
//        获得查询条件对象
        TbItemCatExample.Criteria criteria = example.createCriteria();
//        设置查询条件
        criteria.andParentIdEqualTo(parentId);
        example.getOredCriteria().add(criteria);
        List<TbItemCat> list = itemCatMapper.selectByExample(example);

        List<EasyUITreeNode> result = new ArrayList<>();
//        遍历查询结果,为EasyUITreeNode进行赋值
        for (TbItemCat tbItemCat : list) {
            EasyUITreeNode node = new EasyUITreeNode();

            node.setId(tbItemCat.getId());
            node.setText(tbItemCat.getName());
//      根据当前节点是否是父节点判断当前节点的开闭
            node.setState(tbItemCat.getIsParent() ? "closed" : "open");
//          将当前节点添加到集合中
            result.add(node);
        }
        return result;
    }
}
